<?php
/**
 * Created by PhpStorm.
 * User: rain
 * Date: 2018/4/7
 * Time: 下午4:22
 */

namespace App\Utils;


use Illuminate\Support\Facades\Cache;
use Overtrue\LaravelWeChat\Facade as EasyWeChat;

class OfficialAccountUtil
{
    private $app;

    public function __construct($name = '')
    {
        $this->app = EasyWeChat::officialAccount($name);
    }

    public function getApp()
    {
        return $this->app;
    }


    /**
     * 获取永久图片素材 mediaId
     * @param string $image 图片地址
     * @return mixed
     */
    public function getForeverImageMediaId($image = 'images/tiancai.jpeg')
    {
        $filePath = public_path($image);
        $cacheKey = md5($filePath);
        $mediaId = Cache::get($cacheKey);
        if (empty($mediaId)){
            $result = $this->app->material->uploadImage($filePath);
            Cache::forever($cacheKey, $result['media_id']);
            $mediaId = $result['media_id'];
        }

        return $mediaId;
    }

    /**
     *  获取临时图片素材 mediaId
     * @param string $image
     * @return mixed
     */
    public function getTempImageMediaId($image = 'images/tiancai.jpeg')
    {
        $filePath = public_path($image);
        $cacheKey = md5($filePath);
        $mediaId = Cache::get($cacheKey);
        if (empty($mediaId)){
            $minutes = 60 * 24 * 2;//两天
            $result = $this->app->material->uploadImage($filePath);
            Cache::add($cacheKey, $result['media_id'], $minutes);
            $mediaId = $result['media_id'];
        }

        return $mediaId;
    }

    /**
     * 创建菜单
     * @return mixed
     */
    public function createMenu()
    {
        $this->app->menu->delete();

        $buttons = [
            [
                'type' => 'view',
                'name' => '马上赚钱',
                'url' => 'https://swzx.lingyiliebian.com/h5/index',
            ],
            [
                'type' => 'view',
                'name' => '组队试玩',
                'url' => 'https://swzx.lingyiliebian.com/h5/collector',
            ],

            [
                'name' => '提现',
                'sub_button' => [
                    [
                        'type' => 'view',
                        'name' => '福利群',
                        'url' => 'http://a.gzliemao.com/c/3aa9cb8e73d1c679ae428ca3587da500',
                    ],
                    [
                        'type' => 'view',
                        'name' => '提现',
                        'url' => 'https://swzx.lingyiliebian.com/h5/my',
                    ]
                ]

            ],

            //[
            //    'type' => 'miniprogram',
            //    'name' => '程序试玩中心',
            //    'url' => 'https://swzx.lingyiliebian.com/',
            //    'appid' => 'wx7ac07da7a8a4acbd',
            //    'pagepath' => 'pages/index/index'
            //],

            //[
            //    "type" => "click",
            //    "name" => "加入宝妈",
            //    "key" => 'image_synthesis'
            //],
        ];


        return $this->app->menu->create($buttons);
    }

    /**
     * 获取菜单列表
     * @return mixed
     */
    public function getMenuList()
    {
        return $this->app->menu->list();
    }


}